doctype html
html
  head
    title Server [#{game.title}]
    meta(http-equiv='content-type', content='text/html; charset=UTF-8')
    meta(name='viewport', content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui=1')
    meta(charset='UTF-8')
    meta(name='description', content=game.description)

    link(rel='icon', type='image/png', href="/"+game.author+"/"+game.pathcode+"/icon64.png")
    link(rel="apple-touch-icon" sizes="180x180" href="/"+game.author+"/"+game.pathcode+"/icon180.png")
    link(rel="icon" type="image/png" sizes="32x32" href="/"+game.author+"/"+game.pathcode+"/icon32.png")
    link(rel="icon" type="image/png" sizes="16x16" href="/"+game.author+"/"+game.pathcode+"/icon16.png")

    script.
      window.url = "#{server_url}" ;
      window.ms_standalone = !{standalone} ;

  body.noselect.custom-cursor(oncontextmenu='return false;')
    div(style="position: fixed ; top:0 ; bottom: 0 ; left: 0 ; right: 0 ;")
      iframe(width="100%" height="100%" src=server_url style="border: none")

  script.
    window.addEventListener("message", (msg) => {
      var data = JSON.parse(msg.data) ;
      if (data.name == "get_token") {
        var token = localStorage.getItem("token") ;
        if (window.ms_standalone) {
          token = "standalone" ;
        }
        if (token) {
          console.info(token) ;
          var socket = new WebSocket(location.origin.replace("http","ws")) ;
          socket.onopen = ()=> {
            var s = location.pathname.split("/") ;
            var path = s[1]+"/"+s[2] ;
            console.info(path) ;
            socket.send(JSON.stringify({
              name: "get_server_token",
              user_token: token,
              server_id: path
            }))
          }

          socket.onmessage = (msg)=> {
            var data = JSON.parse(msg.data) ;
            if (data.token != null) {
              document.querySelector("iframe").contentWindow.postMessage(JSON.stringify({
                name: "set_token",
                token: data.token
              }),"*")
              socket.close() ;
            }
          }
        }
      }
    })
    
